package com.fcc.wfly.printhead.controller;

import com.fcc.wfly.base.controller.RestApiController;
import com.fcc.wfly.common.domain.ErrorHandleConst;
import com.fcc.wfly.printhead.domain.MaintenanceDetail;
import com.fcc.wfly.printhead.hal.MaintenanceTasksDetailJson;
import com.fcc.wfly.printhead.service.PrintheadService;
import com.fcc.wfly.printhead.hal.MaintenanceTasksJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class GetMaintenanceTasksController extends RestApiController<MaintenanceTasksJson> {

	private static final String API_NAME = "GetMaintenanceTasks";
	private static final String API_PARAM_PN = "PN";
	private static final String API_PARAM_SN = "SN";
	private static final String API_PARAM_DATE = "date";

	private static SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");

	@Autowired
	private PrintheadService printheadService;

	@RequestMapping(API_NAME)
	public HttpEntity<MaintenanceTasksJson> getDeviceUtilization(HttpServletRequest request, HttpServletResponse response) {
		return process(request, response);
	}

	@Override
	protected String getApiName() {
		return API_NAME;
	}

	@Override
	protected String[] getApiParamNames() {
		return new String[] { API_PARAM_PN, API_PARAM_SN, API_PARAM_DATE };
	}

	@Override
	protected MaintenanceTasksJson process(Map<String, String> params) {
		String PN = params.get(API_PARAM_PN);
		String SN = params.get(API_PARAM_SN);
		String date = params.get(API_PARAM_DATE);
		ErrorHandleConst.MaintenanceTasksParamsValidate(PN,SN,date);
		return process(PN, SN, date);
		}

	private MaintenanceTasksJson process(String PN, String SN, String date) {
		MaintenanceTasksJson result = new MaintenanceTasksJson();
		result.setProduct_Number(PN);
		result.setSerial_Number(SN);

		List<MaintenanceDetail> maintenanceDetailList = printheadService.getMaintenanceDetailTaskList(SN, date.substring(0, 10));
		List<MaintenanceTasksDetailJson> resultDetail = new ArrayList<MaintenanceTasksDetailJson>();
		for (MaintenanceDetail maintenanceDetail : maintenanceDetailList) {
			MaintenanceTasksDetailJson maintenance = new MaintenanceTasksDetailJson();
			maintenance.setName(maintenanceDetail.getPartName());
			maintenance.setDate(getYYYY_MM_DD(maintenanceDetail.getLastUpdate()));
			maintenance.setProgress_Percentage(getProgress(maintenanceDetail.getFreqDays(), maintenanceDetail.getCounter()));
			maintenance.setStatus(maintenanceDetail.getSeverity());
			maintenance.setEstimated_date_trigger(getDate(maintenanceDetail.getEstimatedDt()));
			maintenance.setUser_replaceable(getUser_replaceable(maintenanceDetail.getPartName()));
			maintenance.setLast_maintenance_date(getDate(maintenanceDetail.getReplaceDate()));
			resultDetail.add(maintenance);
		}
		result.setList_maintenances(resultDetail);
		return result;
	}

	protected static SimpleDateFormat yyyyMMddHH = new SimpleDateFormat("yyyyMMddHH");

	private static String getYYYY_MM_DD(String lastUpdate) {
		try {
			return yyyyMMdd.format(yyyyMMddHH.parse(lastUpdate));
		} catch (ParseException e) {
			System.out.println(e);
		}
		return "";
	}

	private static String getDate(String date) {
		try {
			return DATEFORMAT_REQUEST.format(Timestamp.valueOf(date));
		} catch (Exception e) {
		}
		return null;
	}

	private static int getProgress(String freqDays, String counter) {
		try {
			return (int) (Float.valueOf(counter) / Float.valueOf(freqDays) * 100);
		} catch (Exception e) {
		}
		return 0;
	}

	private static String[] USER_REPLACEABLE_FALSE = { "Latex1000Series_3000liters_Duplicated", "Latex1000Series_3000liters", "Latex3000_Service_Duplicated", "Latex3000_Service", "TextileService_Replace" };

	private static boolean getUser_replaceable(String partName) {
		for (String falseName : USER_REPLACEABLE_FALSE) {
			if (falseName.equals(partName)) {
				return false;
			}
		}
		return true;
	}

}